//
//  Card.swift
//  tappay_charge
//
//  Created by Warren L on 2024/7/22.
//

import TPDirect

typealias CardPayPaymentCallback = (TappayPaymentResult) -> Void

class CardPay: NSObject {
    /// 驗證卡號是否可用
    func validateCard(
        cardNumber: String,
        dueMonth: String,
        dueYear: String,
        ccv: String
    ) -> Bool {
        guard let result = TPDCard.validate(withCardNumber: cardNumber, withDueMonth: dueMonth, withDueYear: dueYear, withCCV: ccv) else {
            return false
        }
        
    
        return result.isCardNumberValid && result.isExpiryDateValid && result.isCCVValid
    }
    
    /// 獲取卡的prime
    func createCardToken(
        cardNumber: String,
        dueMonth: String,
        dueYear: String,
        ccv: String,
        callback: CardPayPaymentCallback?
    ) {
        
        print(cardNumber)
        print(dueMonth)
        print(dueYear)
        print(ccv)
        
        TPDCard
            .setWithCardNumber(cardNumber, withDueMonth: dueMonth, withDueYear: dueYear, withCCV: ccv)
            .onSuccessCallback { prime, cardInfo, cardIdentifier, merchantReferenceInfo in
                let successResult = TappayPaymentResult(success: true, status: nil, message: "Get card prime was successful", prime: prime)
                callback?(successResult)
            }
            .onFailureCallback { status, message in
                let errorResult = TappayPaymentResult(success: false, status: status, message: message, prime: nil)
                callback?(errorResult)
            }
            .createToken(withGeoLocation: "UNKNOWN")
        
    }
}
